package com.demo.lifeinfo.app;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.demo.lifeinfo.R;
import com.demo.lifeinfo.model.GlideApp;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.impl.LoadingPopupView;

import butterknife.ButterKnife;
import me.yokeyword.fragmentation.SupportFragment;

/**
 * created by Raymond
 * on 2019-04-24
 */
public abstract class XBaseFragment extends SupportFragment {

    private LoadingPopupView loadingPopupView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        return inflater.inflate(getLayoutId(), null);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        ButterKnife.bind(this, getView());
        initView();
        initData();
    }

    @LayoutRes
    public abstract int getLayoutId();

    public abstract void initView();

    public abstract void initData();

    public void showProgressDialog() {
        if (loadingPopupView == null) {
            loadingPopupView = new XPopup.Builder(getActivity()).asLoading();
        }
        loadingPopupView.show();
    }

    public void dimssProgressDialog() {
        if (loadingPopupView != null)
            loadingPopupView.dismiss();
    }

    @SuppressLint("ResourceAsColor")
    public void showError(String text) {

        ToastUtils.setGravity(Gravity.CENTER, 0, 0);
        ToastUtils.setMsgTextSize(16);
        ToastUtils.showLong(text);
    }

    public void loadImage(String path, ImageView img) {
        GlideApp.with(this).load(path).centerCrop().placeholder(R.drawable.img_wait).error(R.drawable.img_fail).into(img);
    }
}
